using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;
using System.Collections.Generic;
using Gameplay.PVE.Utils;

namespace Gameplay.PVE.TurnBase
{
    public class BattleComponennt : IComponent,ITransmit
    {
        //战斗组件
        private BattleUnit unit;
        private Dictionary<int, SkillInitiative> skills;
        private SkillInitiative currentSkill;
        private int normalSkillId;
        private int initiativeSkillId;
        private ElementSystem elementSystem;
        private PassiveSystem passiveSystem;
        private SkillTemporarySystem temporarySystem;
        

        void IClass.OnReset()
        {
            if (elementSystem != null)
            {
                ClassManager.Free(elementSystem);
            }
            elementSystem = null;
        }

        void IComponent.OnInitialize(BattleUnit unit)
        {
            this.unit = unit;
        }

        void IComponent.OnUpdate()
        {
            elementSystem.Update();
            passiveSystem.Update();
            temporarySystem?.Update();
            currentSkill?.Update();
            unit.Data.IsInAction = currentSkill != null && currentSkill.IsInAction();
            unit.Data.IsCasting = currentSkill != null && currentSkill.IsCasting();
        }

        void IComponent.OnBirth()
        {
            InitSkills();
            elementSystem = ClassManager.Get<ElementSystem>();
            elementSystem.Init();
            if (unit.Data.team == 2)
            {
                unit.Play("Cele");
            }
        }

        void IComponent.OnDead()
        {
            elementSystem.OnDead();
        }
        
        
        void ITransmit.OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (type == ETransmitType.Attack)
            {
                currentSkill = skills[normalSkillId];
                currentSkill.Start();
            }
            else if (type == ETransmitType.Skill)
            {
                unit.Data.CurrentMp = 0;
                int id = initiativeSkillId;
                if (id  == 0)
                {
                    id  = normalSkillId;
                }
                currentSkill = skills[id];
                if (id != normalSkillId)
                {
                    //PveFlyTextManager.Instance.ShowFlyText(currentSkill.config.name,unit.Data.position + new Vector3(0,0.5f,0),0.8f,0.3f);
                }
                currentSkill.Start();
            }
            else if (type == ETransmitType.AddElement)
            {
                var elementArg = arg as AddElementArg;
                var config = PveUtils.GetTbsSkillElementConfig(elementArg.elementId);
                /*var position = unit.Data.position +
                               PveUtils.globalRatio * new Vector3(Random.Range(-0.3f,0.3f), Random.Range(-0.2f,0.7f), Random.Range(-0.3f,0.3f)) + new Vector3(0,PveUtils.globalRatio,0);
                PveEffectManager.Instance.PlayEffect(1,position,unit.Data.forward,0.3f);
                PveFlyTextManager.Instance.ShowFlyText(config.output.ToString(),position + new Vector3(0,0.1f,0),1f,0.7f);*/
                

                if (config.timeStop > 0)
                {
                    //TurnBaseManager.Instance.TimeStop(config.timeStop / 1000f);
                }

                if (config.shake == 1)
                {
                    TurnBaseManager.Instance.Shake(0.2f);
                }

                if (elementArg.source.Data.team == 0)
                {
                    TurnBaseManager.Instance.AddHit();
                }

                elementSystem?.AddElement(unit, elementArg.source, config);
            }
            else if (type == ETransmitType.Reborn)
            {
                unit.Data.isDead = false;
                unit.Data.CurrentHp = unit.Data.TotalHp;
                unit.Data.CurrentMp = 0;
                unit.Play("Idle");
            }
            else if (type == ETransmitType.BeforeRound)
            {
                if (unit.Data.controlList.Count > 0)
                {
                    int control = unit.Data.controlList[0];
                    if (control == 1)//冰冻
                    {
                        unit.Data.RestMoveCount -= 1;
                    }
                    else if (control == 2)// 麻痹
                    {
                        if (Random.Range(0, 1f) < 0.5f)
                        {
                            //PveFlyTextManager.Instance.ShowFlyText("麻痹无法行动",unit.Data.position,1,0.3f,ETextColor.White);
                            unit.Data.RestMoveCount -= 1;
                        }
                    }
                }
            }
            elementSystem?.OnTransmit(type);
            passiveSystem?.OnTransmit(type, arg);
            temporarySystem?.OnTransmit(type,arg);
        }

        private void InitSkills()
        {
            skills = new Dictionary<int, SkillInitiative>();
            normalSkillId = unit.Data.config.attackId;
            var skill = ClassManager.Get<SkillInitiative>();
            skill.Init(unit,normalSkillId);
            skills.Add(normalSkillId,skill);

            initiativeSkillId = unit.Data.config.skillId;
            if (initiativeSkillId != 0)
            {
                skill = ClassManager.Get<SkillInitiative>();
                skill.Init(unit,initiativeSkillId);
                skills.Add(initiativeSkillId,skill);
            }
            
            
            passiveSystem = ClassManager.Get<PassiveSystem>();
            passiveSystem.Init(unit);

            temporarySystem = ClassManager.Get<SkillTemporarySystem>();
            temporarySystem.Init(unit);
        }
    }
}